package com.Oct_TwentyNight;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 *
 */

public class BSTIterator {

    TreeNode list=null;

    public BSTIterator(TreeNode root) {
        list=new TreeNode();
        dfs(root);
    }

    void dfs(TreeNode root)
    {
        if (root.right!=null)
        {dfs(root.right);}
        root.right=list;
        list=root;
        //System.out.println(list);
        if (root.left!=null)
        {dfs(root.left);}
    }

    public int next() {
        int val= list.val;
        list=list.right;
        return val;
    }

    public boolean hasNext() {
        System.out.println(List.of(list.val));
        return list!=null;
    }

    public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
}
